package com.junqson.je.user.controller;

import com.junqson.je.common.UserHolder;
import com.junqson.je.common.annotation.LoginRequire;
import com.junqson.je.common.model.ApiResult;
import com.junqson.je.common.model.User;
import com.junqson.je.common.model.dto.AuthInfoDTO;
import com.junqson.je.common.model.dto.AuthResultDTO;
import com.junqson.je.user.client.AdminClient;
import com.junqson.je.user.model.dto.UserDTO;
import com.junqson.je.user.service.UserService;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * @Descriptions
 * @Company
 * @Author Junqson
 * @Date 2020/1/24 22:51
 * @Version 1.0
 */
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    AdminClient adminClient;

    @RequestMapping("/loadUser")
    public ApiResult loadUser(Integer userId) {

        if (userId == null || userId < 0) {
            return ApiResult.error("userId不存在或非法");
        }
        User user = userService.loadUserCache(userId);
        return ApiResult.success(user);
    }

    /**
     * 查看自己的信息
     *
     * @return
     */
    @LoginRequire
    @RequestMapping("/myinfo")
    public ApiResult myinfo() {
        User user = UserHolder.get();
        return ApiResult.success(userService.getSimpleUserInfo(user.getUserId()));
    }


    /**
     * 查看用户个人主页(可公开)信息
     *
     * @return
     */
    @RequestMapping("/userInfo")
    public ApiResult userInfo(Integer userId) {
        if (userId == null) {
            return ApiResult.error("user id is null");
        }
        log.info("get user info, user id {}", userId);
        return ApiResult.success(userService.getSimpleUserInfo(userId));
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @LoginRequire
    @RequestMapping("/updateUserInfo")
    public ApiResult updateUserInfo(@RequestBody UserDTO user) {
        User ruser = UserHolder.get();
        if (user != null) {
            user.setUserId(ruser.getUserId());
            userService.updateUserInfo(user);
        }

        return ApiResult.success();
    }

    /**
     * 认证用户企业身份请求
     *
     * @return
     */
    @LoginRequire
    @RequestMapping("/userAuth")
    public ApiResult authUser(@RequestBody AuthInfoDTO authInfoDTO) {

        if (authInfoDTO == null) {
            return ApiResult.error("auth info is null");
        }

        User user = UserHolder.get();
        if (user == null) {
            return ApiResult.error("user is null");
        }
        int userId = user.getUserId();
        authInfoDTO.setUserId(userId);

        return adminClient.createUserAuthOrder(authInfoDTO);
    }


    /**
     * 认证结果回调
     *
     * @return
     */
    @RequestMapping("/authResultCallback")
    public ApiResult userAuthPass(@RequestBody AuthResultDTO resultDTO) throws IOException, TemplateException {
        if (resultDTO.getUserId() == null || !resultDTO.isExamine()) {
            return ApiResult.error("AuthResultDTO error");
        }
        log.info("auth dto {}", ToStringBuilder.reflectionToString(resultDTO));
        if (resultDTO.isPass()) {
            log.info("pass user auth, user id = {}", resultDTO.getUserId());
            userService.passUserAuth(resultDTO);
        } else {
            log.info("user auth failed, user id = {}", resultDTO.getUserId());
            userService.userAuthFailed(resultDTO);
        }
        return ApiResult.success();
    }

    @Value("${app-name}")
    private String appName;

    @PostConstruct
    public void printName() {
        log.info("=========================");
        log.info(appName);
        log.info("=========================");
    }


}
